package com.hsc.www.reSend;

import com.alibaba.fastjson.JSONObject;
import com.hsc.www.reSend.core.Config;
import com.hsc.www.reSend.core.delayedQueue.CapillaryDelayedQueue;
import com.hsc.www.reSend.core.delayedQueue.CapillaryDelayedQueueFactory;
import com.hsc.www.reSend.core.delayedQueue.DefaultCapillaryDelayedQueue;
import com.hsc.www.reSend.core.delayedQueue.DefaultCapillaryDelayedQueueFactory;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Configuration
public class Config1 {

    @Bean
    public CapillaryDelayedQueueFactory capillaryDelayedQueueFactory() {
        List<CapillaryDelayedQueue> queueList = Arrays.asList(
                new DefaultCapillaryDelayedQueue(5, TimeUnit.SECONDS, "capillary_queue_seconds_05"),
                new DefaultCapillaryDelayedQueue(10, TimeUnit.SECONDS, "capillary_queue_seconds_10"),
                new DefaultCapillaryDelayedQueue(30, TimeUnit.SECONDS, "capillary_queue_seconds_30"),
                new DefaultCapillaryDelayedQueue(1, TimeUnit.MINUTES, "capillary_queue_minutes_01")
//                new DefaultCapillaryDelayedQueue(15, TimeUnit.MINUTES, "capillary_queue_minutes_15"),
//                new DefaultCapillaryDelayedQueue(30, TimeUnit.MINUTES, "capillary_queue_minutes_30"),
//                new DefaultCapillaryDelayedQueue(1, TimeUnit.HOURS, "capillary_queue_hours_01"),
//                new DefaultCapillaryDelayedQueue(5, TimeUnit.HOURS, "capillary_queue_hours_05"),
//                new DefaultCapillaryDelayedQueue(1, TimeUnit.DAYS, "capillary_queue_day_01"),
//                new DefaultCapillaryDelayedQueue(5, TimeUnit.DAYS, "capillary_queue_day_05")
        );
        return new DefaultCapillaryDelayedQueueFactory(queueList);
    }



    @Bean
    public Queue businessQueue() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", "");
        arguments.put("x-dead-letter-routing-key", Config.DELAYED_MESSAGE_REGISTRAR_QUEUE);
        return new Queue("businessQueue", true, false, false, arguments);
    }

    @RabbitListener(queues = "businessQueue")
    public void handleDelayedMessage11(Channel channel, Message message) throws Exception {
        System.out.println("消费消息：" + JSONObject.toJSONString(message));
        channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);

    }

}